<?php
/**
 * 电子邮件
 * @author Administrator
 *
 */
class Application_Model_Mail
{
	private static $accountList = array(
		
	);
	
	/**
	 * 枚举全部账号信息
	 */
	public static function enumAccount()
	{
		$mailconfM = new Application_Model_DbTable_Mailconf();
		$accoutLst = $mailconfM->getEnabledItems();
		foreach ($accoutLst as $a){
			self::$accountList[$a->email] = $a->toArray();
		}
		return self::$accountList;
	}
	
	/**
	 * 发送邮件，将数据缓冲区中标记为未发送*（或发送失败）的邮件（重新）发送
	 */
	public static function sendAll()
	{
		$mailOutBoxM = new Application_Model_DbTable_Mailoutbox();
		$mailList = $mailOutBoxM->getAllNeedSend();
	}
	
	/**
	 * 重新匹配全部客户邮件
	 * @param bool $isAll 是否全部重新匹配
	 */
	public function matchCustomer($isAll=FALSE)
	{
		$mailInboxM = new Application_Model_DbTable_Mailinbox();
		$customerM	= new Application_Model_DbTable_Customer();
		$custagentM = new Application_Model_DbTable_Custagent();
		
		$mailSelect = $mailInboxM->select();
		if(!$isAll){
			$mailSelect->where(" `customer` IS NULL ");
		}
		$mailSelect->where(" `deleted` = 0 ");
		foreach ($mailInboxM->fetchAll($mailSelect) as $mail){
			try {
			if($customer = $customerM->getItemByMail($mail->from)){
				$customer = $customer->id;
			}elseif ($customer = $custagentM->getCustomerByMail($mail->from)){
				$customer = $customer->id;
			};
			if(!empty($customer)){
				$data["customer"] = $customer;
				$mailInboxM->update($data, " `id`={$mail->id} ");
			}
			}catch (Exception $e){
				continue;
			}
		}		
	}
}